package com.android.scj.tc;

import java.util.Calendar;
import java.util.NoSuchElementException;
import android.annotation.SuppressLint;
import android.os.Bundle;

import com.equipment.ActivateOBURequest;
import com.equipment.ActivateOBUResponse;
import com.equipment.CardCommandChannelRequest;
import com.equipment.CardCommandChannelResponse;
import com.equipment.ChineseActivateOBURequest;
import com.equipment.ChineseActivateOBUResponse;
import com.equipment.ChineseDetectionOBURequest;
import com.equipment.ChineseDetectionOBUResponse;
import com.equipment.CloseOBUConnectionRequest;
import com.equipment.CloseOBUConnectionResponse;
import com.equipment.ColdResetRequest;
import com.equipment.ColdResetResponse;
import com.equipment.DetectionOBURequest;
import com.equipment.DetectionOBUResponse;
import com.equipment.ESAMReductionRequest;
import com.equipment.ESAMReductionResponse;
import com.equipment.GetRandomRequest;
import com.equipment.GetRandomResponse;
import com.equipment.HardwareParamTextRequest;
import com.equipment.HardwareParamTextResponse;
import com.equipment.IPORequest;
import com.equipment.IPOResponse;
import com.equipment.OBUCardCommandChannelRequest;
import com.equipment.OBUCardCommandChannelResponse;
import com.equipment.OBUTransactionRequest;
import com.equipment.OBUTransactionResponse;
import com.equipment.ReadChineseVehicleRequest;
import com.equipment.ReadChineseVehicleResponse;
import com.equipment.ReadSysRequest;
import com.equipment.ReadSysResponse;
import com.equipment.ReadSystemRequest;
import com.equipment.ReadSystemResponse;
import com.equipment.ReadVehicleRequest;
import com.equipment.ReadVehicleResponse;
import com.equipment.SearchOBURequest;
import com.equipment.SearchOBUResponse;
import com.equipment.SetParameterRequest;
import com.equipment.SetParameterResponse;
import com.equipment.UpdateInformationRequest;
import com.equipment.UpdateInformationResponse;
import com.equipment.WAVELOGRequest;
import com.equipment.WAVELOGResponse;
import com.equipment.WaveSendRequest;
import com.equipment.WaveSendResponse;
import com.equipment.WriteSysInfoRequest;
import com.equipment.WriteSysInfoResponse;
import com.equipment.WriteVehicleInfoRequest;
import com.equipment.WriteVehicleInfoResponse;

import com.util.EncodingUtil;

/**
 * 终端通信Service
 *
 * @author 刘亚东
 *
 */
@SuppressLint("DefaultLocale")
public class TerminalService {

	private static TerminalService service;
	public static Bundle bun; // 封装OBU数据
	public static Bundle bundle; // 封装CPU卡数据
	public static Bundle NewBun;
	private String type = "0";// 0代表普通读卡解析 1代表需要解析卡片信息和余额信息
	public static String isExist = "1";// 卡片是否存在 0表示存在 1表示不存在
	public static String OBUMAC = "";
	public static int checkCard = 0;// 0表示复位信息A3(为了保存后台查询的卡信息) 1表示预读信息A3
	public static String vehicleCardBing = "";
	public static String CardNumber = "";




	/**
	 * 返回一个共用的Service
	 *
	 * @return
	 */
	public static TerminalService getService() {
		if (service == null)
			return new TerminalService();
		return service;
	}
	/**
	 * 拼接C0
	 * @param contractProvider2
	 * @param contractType
	 * @param contractVersion
	 * @param contractSerialNumber
	 * @return
	 */
	public String sendC0(String contractProvider2,String contractType,String contractVersion,String contractSerialNumber){
		String str = Connection.map.get("Replace");
		if(str!=null && str.length()>1 && !"".equals(str)){
			if(str.length()<2){
				str="0"+str;
			}
		}else{
			str = "01";
		}
		return new IPORequest().getIPORequest(str, contractProvider2, contractType, contractVersion, contractSerialNumber);
	}
	/**
	 * 解析D0一次发行应答
	 * @param context
	 * @throws Exception
	 */
	public void adoptD0(String context) throws Exception{
		new IPOResponse().getIpoResponse(context);
	}

	/**
	 * 拼接C1数据帧
	 */
	public String sendC1(){
		return new ReadSysRequest().command();

	}
	/**
	 * 解析C1数据帧
	 */
	public void adoptD1(String context) throws NoSuchElementException{
		ReadSysResponse rr = new ReadSysResponse();
		rr.analysis(context);
		String[] strs = null;
		try {
			strs = EncodingUtil.change(rr.getSysInfo()).split(" ");
			// 开始装载数据
			NewBun = new Bundle();
			NewBun.putString("ID", strs[0]+strs[1]+strs[2]+strs[3]+strs[4]+strs[5]+strs[6]+strs[7]);
			NewBun.putString("Entente", strs[8]);
			NewBun.putString("Version", strs[9]);
			NewBun.putString("VersionNumber", strs[10]+strs[11]+strs[12]+strs[13]+strs[14]+strs[15]+strs[16]+strs[17]);
			NewBun.putString("SignedTime", strs[18]+strs[19]+strs[20]+strs[21]);
			NewBun.putString("DueTime", strs[22]+strs[23]+strs[24]+strs[25]);
			strs = null;
			rr = null;
		} catch (Exception e) {
			e.printStackTrace();
			strs = null;
			rr = null;
		}

	}
	/**
	 * 国标读车辆信息C2
	 * @return
	 */
	public String sendC2(){
		String city = Connection.map.get("City");
		String [] strs=EncodingUtil.getCityIndex(city);
		return new ReadChineseVehicleRequest().getReadChineseVehicleRequest(strs[2]);
	}
	/**
	 * 解析C2应答D2
	 * @param context
	 */
	public void adoptD2(String context){
		ReadChineseVehicleResponse rr = new ReadChineseVehicleResponse();
		rr.analysis(context);
		String[] strs = null;
		try {
			strs = EncodingUtil.change(rr.getVehicleInfo()).split(" ");
			// 开始装载数据
			NewBun = new Bundle();
			try {
				NewBun.putString("VehicleName", EncodingUtil.asciiToString(strs[0]+strs[1]+strs[2]+strs[3]+strs[4]+strs[5]+strs[6]+strs[7]+strs[8]+strs[9]+strs[10]+strs[11]));
			} catch (Exception e) {
				NewBun.putString("VehicleName", "");
			}
			NewBun.putString("VehicleColor", EncodingUtil.hexToDec(strs[12]+strs[13]));

			NewBun.putString("VehicleType", EncodingUtil.hexToDec(strs[14]));
			NewBun.putString("VehicleUserType",  EncodingUtil.hexToDec(strs[15]));
			NewBun.putString("VehicleLength", EncodingUtil.hexToDec(strs[16]+strs[17]));
			NewBun.putString("VehicleWide",EncodingUtil.hexToDec(strs[18]));
			NewBun.putString("VehicleHigh",EncodingUtil.hexToDec(strs[19]));
			NewBun.putString("VehicleWheel",EncodingUtil.hexToDec(strs[20]));
			NewBun.putString("VehicleAxle", EncodingUtil.hexToDec(strs[21]));
			NewBun.putString("VehicleWheelBase", EncodingUtil.hexToDec(strs[22]+strs[23]));
			NewBun.putString("VehicleLoadNum", EncodingUtil.hexToDec(strs[24]+strs[25]+strs[26]));
			try {
				NewBun.putString("VehicleFeatures", EncodingUtil.asciiToUTFString(strs[27]+strs[28]+strs[29]+strs[30]+strs[31]+strs[32]+strs[33]+strs[34]+strs[35]+strs[36]+strs[37]+strs[38]+strs[39]+strs[40]+strs[41]+strs[42]));
				System.err.println("特征："+strs[27]+strs[28]+strs[29]+strs[30]+strs[31]+strs[32]+strs[33]+strs[34]+strs[35]+strs[36]+strs[37]+strs[38]+strs[39]+strs[40]+strs[41]+strs[42]);
			} catch (Exception e) {
				NewBun.putString("VehicleFeatures", "");
			}
			try {
				NewBun.putString("VehicleEngine",EncodingUtil.asciiToUTFString(strs[43]+strs[44]+strs[45]+strs[46]+strs[47]+strs[48]+strs[49]+strs[50]+strs[51]+strs[52]+strs[53]+strs[54]+strs[55]+strs[56]+strs[57]+strs[58]));
				System.err.println("發動機："+strs[43]+strs[44]+strs[45]+strs[46]+strs[47]+strs[48]+strs[49]+strs[50]+strs[51]+strs[52]+strs[53]+strs[54]+strs[55]+strs[56]+strs[57]+strs[58]);
			} catch (Exception e) {
				NewBun.putString("VehicleEngine","");
			}
			strs = null;
			rr = null;
		} catch (Exception e) {
			e.printStackTrace();
			strs = null;
			rr = null;
		}
	}

	/**
	 * 拼接国标写系统信息C3
	 * @param sysInfo
	 * @return
	 */
	public String sendC3(String sysInfo){
		String city = Connection.map.get("City");
		String [] strs=EncodingUtil.getCityIndex(city);
		return new WriteSysInfoRequest().getWriteSysInfo(strs[0],strs[1], sysInfo);
	}
	/**
	 * 解析D3写系统信息应答
	 * @param context
	 * @throws Exception
	 */
	public void adoptD3(String context) throws Exception{
		WriteSysInfoResponse wr=new WriteSysInfoResponse();
		wr.analysis(context);
	}
	/**
	 * 写车辆信息C4
	 * @param vehicle
	 * @return
	 */
	public String sendC4(String vehicle){
		String city = Connection.map.get("City");
		String [] strs=EncodingUtil.getCityIndex(city);
		return new WriteVehicleInfoRequest().getWriteVehicleInfo(strs[0],strs[1], vehicle);
	}
	/**
	 * 解析D4写车辆信息应答
	 * @param context
	 * @throws Exception
	 */
	public void adoptD4(String context) throws Exception{
		WriteVehicleInfoResponse wr= new WriteVehicleInfoResponse();
		wr.setWriteVehicleInfoResponse(context);
	}
	/**
	 * 国标脱机检测C5
	 * @return
	 */
	public String sendC5(){
		String city = Connection.map.get("City");
		String [] strs=EncodingUtil.getCityIndex(city);
		return new ChineseDetectionOBURequest().command(strs[2]);
	}
	/**
	 * 解析国标脱机检测应答D5
	 * @param context
	 * @throws NoSuchElementException
	 */
	public void adoptD5(String context) throws NoSuchElementException {
		ChineseDetectionOBUResponse dr = new ChineseDetectionOBUResponse();
		dr.analysis(context);
		bun = new Bundle();
		bundle = new Bundle();
		try {
			// 取高字节转换八位二进制
			String is = EncodingUtil.getBooleanArray(Byte.parseByte(dr
					.getOBUStatus().substring(0, 2)));
			bun = new Bundle();
			bundle = new Bundle();
			// 没有系统信息
			String tagNumbers = "未定义";
			String OBUContract = tagNumbers;
			String OBUSecret = tagNumbers;
			String tagValidity_1 = tagNumbers;
			String tagValidity_2 = tagNumbers;
			if (dr.getSystemInfoLen().equals("0")) {
				tagNumbers = "";
				OBUContract = tagNumbers;
				OBUSecret = tagNumbers;
				tagValidity_1 = tagNumbers;
				tagValidity_2 = tagNumbers;
			} else {

				// 标签号码 系统信息合约序列号11-18字节
				try {
					tagNumbers = dr.getSysInfo().substring(20, 36);
				} catch (Exception e) {
					tagNumbers = "未定义";
				}
				try {
					// 标签有效期 系统信息的19-26字节
					tagValidity_1 = EncodingUtil.convert(dr.getSysInfo()
							.substring(36, 52));
					tagValidity_2 = tagValidity_1.substring(11);
					tagValidity_1 = tagValidity_1.substring(0, 11);
				} catch (Exception e) {
					tagValidity_1 = "未定义";
					tagValidity_2 = tagValidity_1;
				}
				try {
					// OBU合同版本 系统信息的第10字节合同版本
					OBUContract = dr.getSysInfo().substring(18, 20);
				} catch (Exception e) {
					OBUContract = "未定义";
				}
				try {
					// OBU密钥版本 系统信息的第10字节合同版本
					OBUSecret = dr.getSysInfo().substring(18, 20);
				} catch (Exception e) {
					OBUSecret = "未定义";
				}

			}
			bun.putString("TAGNUMBERS1", tagNumbers);
			bun.putString("TAGVALIDITY1", tagValidity_1);
			bun.putString("TAGVALIDITY2", tagValidity_2);
			bun.putString("OBUCONTRACT1", OBUContract);
			bun.putString("OBUSECRET1", OBUSecret);
			// 如果没有车辆信息
			String vehicleNumber = "未定义";
			String vehicleType = vehicleNumber;
			String vehicleColour = vehicleNumber;
			if (dr.getVehicleInfoLen().equals("0")) {
				vehicleNumber = "";
				vehicleType = vehicleNumber;
				vehicleColour = vehicleNumber;
			} else {
				try {
					// 签内车牌号 车辆信息的1-12字节车牌信息
					vehicleNumber = EncodingUtil.asciiToString(dr
							.getVehicleInfo().substring(0, 24));
				} catch (Exception e) {
					vehicleNumber = "未定义";
				}
				try {
					// 车牌颜色 车辆信息13-14个字节
					vehicleColour = Integer.parseInt(dr.getVehicleInfo()
							.substring(24, 28)) + "";
				} catch (Exception e) {
					vehicleColour = "未定义";
				}
				try {
					// 车型 车辆信息的15字节
					vehicleType = EncodingUtil.hexToDec(dr.getVehicleInfo()
							.substring(28, 30), 2);
				} catch (Exception e) {
					vehicleType = "未定义";
				}
			}
			bun.putString("VEHICLENUMBER1", vehicleNumber);
			bun.putString("VEHICLETYPE1", vehicleType);
			bun.putString("VEHICLECOLOUR1", vehicleColour);
			// 拆卸状态 OBUStatus的第一字节的Bit1位，false：未拆，true：拆卸
			bun.putString("OBUSTATUS1", is.substring(6, 7));
			// 电池电量 OBUStatus的第一字节的Bit0位，false：正常，true：低电
			bun.putString("OBUBATTERY", is.substring(7));
			// OBU MAC地址 BA指令返回的4字节OBUMAC
			bun.putString("OBUMAC1", dr.getObuMac());

			String vehicleCardBinding = "未定义";
			String cardBindingVehicle = vehicleCardBinding;
			String cardValidity_1 = vehicleCardBinding;
			String cardValidity_2 = vehicleCardBinding;
			String cardSecret = vehicleCardBinding;
			String cardNumber = vehicleCardBinding;
			String cardType = vehicleCardBinding;
			if (dr.getCardInfoLen().equals("0")) {
				// 没有卡片信息
				vehicleCardBinding = "";
				cardBindingVehicle = vehicleCardBinding;
				cardValidity_1 = vehicleCardBinding;
				cardValidity_2 = vehicleCardBinding;
				cardSecret = vehicleCardBinding;
				cardNumber = vehicleCardBinding;
				cardType = vehicleCardBinding;
			} else {
				try {
					// 车卡绑定 卡片信息的41字节 0：普通用户 01：车卡绑定
					vehicleCardBinding = dr.getCardInfo().substring(80, 82);
				} catch (Exception e) {
					vehicleCardBinding = "未定义";
				}

				try {
					// 卡内车牌号 卡信息29-40字节的车牌信息
					cardBindingVehicle = EncodingUtil.asciiToString(dr
							.getCardInfo().substring(56, 80));
				} catch (Exception e) {
					cardBindingVehicle = "未定义";
				}
				try {
					// 卡片有效期 卡片信息的21-28字节
					cardValidity_1 = EncodingUtil.convert(dr.getCardInfo()
							.substring(40, 56));
					cardValidity_2 = cardValidity_1.substring(11);
					cardValidity_1 = cardValidity_1.substring(0, 11);

				} catch (Exception e) {
					cardValidity_2 = "未定义";
					cardValidity_1 = cardValidity_2;
				}
				try {
					// 卡片密钥版本 卡片信息中的第10字节
					cardSecret = dr.getCardInfo().substring(18, 20);
				} catch (Exception e) {
					cardSecret = "未定义";
				}
				try {
					// 卡号 卡信息的13-20字节CPU卡片内部编号
					cardNumber = dr.getCardInfo().substring(20, 40);
				} catch (Exception e) {
					cardNumber = "未定义";
				}
				try {
					// 卡类型 卡信息的第9字节卡类型
					cardType = EncodingUtil.hexToDec(dr.getCardInfo()
							.substring(16, 18), 2);
				} catch (Exception e) {
					cardType = "未定义";
				}
			}
			bundle.putString("CARDBINDINGVEHICLE0", vehicleCardBinding);
			bundle.putString("CARDBINDINGVEHICLE1", cardBindingVehicle);
			bundle.putString("CARDVALIDITY1", cardValidity_1);
			bundle.putString("CARDVALIDITY2", cardValidity_2);
			bundle.putString("CARDSECRET1", cardSecret);
			bundle.putString("CARDNUMBER1", cardNumber);
			bundle.putString("CARDTYPE1", cardType);

			String balance = "0";
			if (dr.getCardRestMoneyLen().equals("0")) {
				// 没有卡内余额信息
				balance = "";
			} else {
				// 余额
				balance = EncodingUtil.hexToDec(dr.getCardRestMoney(), 0);
				if (balance.length() > 6) {
					String src = balance.substring(balance.length() - 2);
					balance = balance.substring(0, balance.length() - 2);
					balance = balance + "." + src + "元";
				} else {
					balance = (Double.parseDouble(balance) / 100) + "元";
				}
			}
			bundle.putString("BALANCE1", balance);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送C6国标脱机激活
	 * @param startTime
	 * @param dueTime
	 * @return
	 */
	public String sendC6(){
		String city = Connection.map.get("City");
		String [] strs=EncodingUtil.getCityIndex(city);

		String startTime = Connection.map.get("startTime");
		String endTime = Connection.map.get("endTime");
		if(startTime==null || "".equals(startTime)){
			startTime = "20030605";
		}
		if(endTime==null || "".equals(endTime)){
			endTime = "20150605";
		}
		return new ChineseActivateOBURequest().getChineseActivateOBURequest(strs[2],strs[0],strs[1],startTime, endTime);
	}
	/**
	 * 解析D6国标脱机激活
	 * @param startTime
	 * @param dueTime
	 * @return
	 */
	public void adoptD6(String context){
		ChineseActivateOBUResponse dr = new ChineseActivateOBUResponse();
		dr.analysis(context);
		bun = new Bundle();
		String[] sysinfo = null;
		String[] vehicleinfo = null;
		String[] cardinfo = null;
		try {
			//系统信息
			if(!dr.getSystemInfoLen().equals("0")){
				sysinfo = EncodingUtil.change(dr.getSysInfo()).split(" ");
				//5、OBU秘钥版本
				bun.putString("OBUSecret01", sysinfo[9]);
				//7、标签号码
				bun.putString("tagNumbers01", sysinfo[10]+sysinfo[11]+sysinfo[12]+sysinfo[13]+sysinfo[14]+sysinfo[15]+sysinfo[16]+sysinfo[17]);
			}
			//车辆信息
			if(!dr.getVehicleInfoLen().equals("0")){
				vehicleinfo = EncodingUtil.change(dr.getVehicleInfo()).split(" ");
				try {
					//1、签内车牌号
					bun.putString("vehicleNumber", EncodingUtil.asciiToString(vehicleinfo[0]+vehicleinfo[1]+vehicleinfo[2]+vehicleinfo[3]+vehicleinfo[4]+vehicleinfo[5]+vehicleinfo[6]+vehicleinfo[7]+vehicleinfo[8]+vehicleinfo[9]+vehicleinfo[10]+vehicleinfo[11]));
				} catch (Exception e) {
					bun.putString("vehicleNumber","");
				}
				//3、车型
				bun.putString("vehicleType01", vehicleinfo[14]);
				//4、车牌颜色
				bun.putString("vehicleColour01", vehicleinfo[12]+vehicleinfo[13]);
			}
			// 卡片信息
			if(!dr.getCardInfoLen().equals("0")){
				cardinfo = EncodingUtil.change(dr.getCardInfo()).split(" ");
				try {
					//2、卡内车牌号
					bun.putString("cardBindingVehicle01", EncodingUtil.asciiToString(cardinfo[28]+cardinfo[29]+cardinfo[30]+cardinfo[31]+cardinfo[32]+cardinfo[33]+cardinfo[34]+cardinfo[35]+cardinfo[36]+cardinfo[37]+cardinfo[38]+cardinfo[39]));
				} catch (Exception e) {
					bun.putString("cardBindingVehicle01","");
				}
				//6、卡片秘钥版本
				bun.putString("cardSecret01", cardinfo[9]);
			}
			sysinfo = null;
			vehicleinfo = null;
			cardinfo = null;
			dr = null;
		}catch(Exception e){
			e.printStackTrace();
			sysinfo = null;
			vehicleinfo = null;
			cardinfo = null;
			dr = null;
		}
	}
	/**
	 * ESAM还原请求
	 * @return
	 */
	public String sendC7() {
		return new ESAMReductionRequest().command();
	}
	/**
	 * 解析D7ESAM还原请求应答
	 * @param context
	 * @return
	 */
	public ESAMReductionResponse adoptD7(String context) {
		return new ESAMReductionResponse().analysis(context);
	}
	/**
	 * 发送C8 OBU交易请求
	 * @param verifyMac2
	 * @param tradeMoney
	 * @param tradeInfo
	 * @return
	 */
	public String sendC8(String verifyMac2,String tradeMoney,String tradeInfo){
		verifyMac2 = EncodingUtil.addzero(verifyMac2, 1);
		tradeMoney = EncodingUtil.addzero(EncodingUtil.desToHex(Integer.parseInt(tradeMoney)), 4);
		tradeInfo = EncodingUtil.addzero(tradeInfo, 40);
		return new OBUTransactionRequest().command(verifyMac2,EncodingUtil.getThisBCDDate(Calendar.getInstance()), tradeMoney, tradeInfo);
	}
	/**
	 * 解析D8 OBU交易应答
	 * @param context
	 * @return
	 */
	public OBUTransactionResponse adoptD8(String context) {
		OBUTransactionResponse otr = new OBUTransactionResponse();
		otr = otr.analysis(context);
		bun = new Bundle();
		try {
			bun.putString("TermTradeNo", otr.getTermTradeNo());
			bun.putString("CardTradeNo", otr.getCardTradeNo());
			bun.putString("TAC", otr.getTAC());
			bun.putString("Balance1", otr.getBalance1());
			bun.putString("Balance2", otr.getBalance2());
			bun.putString("Money", Integer.parseInt(OBUTransactionRequest.TradeMoney)+"");
			bun.putString("TerTimer", OBUTransactionRequest.TradeTime);
		} catch (Exception e) {
			e.printStackTrace();
			bun = null;
		}
		return otr;
	}

	/**
	 * 发送A0获取设备状态版本
	 *
	 * @return
	 */
	public String sendA0() { //
		return "55AA000001FDA05C";
	}

	/**
	 * 解析B0设备状态版本
	 *
	 * @param context
	 * @return
	 */
	public String adoptB0(String context) throws NoSuchElementException {
		// 判断状态是否成功
		if (!context.substring(14, 16).equals("00")) {
			throw new NoSuchElementException("获取设备版本失败");
		}
		String bcc = context.substring(context.length() - 2).toUpperCase();
		// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
		String str = context.substring(4, context.length() - 2);
		// 得到比对校验
		str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
		// 判断校验是否一致
		if (!str.equals(bcc)) {
			throw new NoSuchElementException("获取设备版本失败");
		}
		//int length = Integer.parseInt(EncodingUtil.hexToDec(context.substring(
		//		16, 18))) * 2;
		String version = EncodingUtil.asciiToString(context.substring(18,
				context.length()-2));
		return version;
	}

	/**
	 * 发送A1指令卡片复位(参数需要传入)
	 *
	 * @param PSAM卡号
	 * @return A1Request
	 */
	public String sendA1(String pasm) {
		return new ColdResetRequest().command(pasm);
	}

	/**
	 * 接受A1指令并解析
	 *
	 * @return A1Response
	 * @throws NoSuchElementException
	 *             错误信息
	 */
	public ColdResetResponse adoptA1(String context)
			throws NoSuchElementException {
		ColdResetResponse cr = new ColdResetResponse().analysis(context);
		return cr;
	}

	/**
	 * 发送A2指令卡命令通道
	 *
	 * @return A2Request
	 * @param pasmNum
	 *            PASM卡号
	 * @param len
	 *            数据内容长度
	 * @param data
	 *            数据内容
	 */
	public String sendA2(String pasmNum, String data) {
		return new CardCommandChannelRequest().command(pasmNum, data);
	}

	/**
	 * 接受并且解析A2指令
	 *
	 * @return A2Response
	 * @throws NoSuchElementException
	 *             错误信息
	 */
	public CardCommandChannelResponse adoptA2(String context)
			throws NoSuchElementException {
		CardCommandChannelResponse cr = new CardCommandChannelResponse()
				.analysis(context);
		return cr;
	}

	/**
	 * 发送A3指令搜索OBU
	 *
	 * @param type
	 *            0表示预读卡信息，钱包信息等；1表示返回OBU系统信息和OBU复位信息等；对于老版本标签（00和10版本）
	 *            如果不支持预读卡信息的需要再次发送AC指令读取卡信息；
	 * @return
	 */
	public String sendA3(String type) {
		return new SearchOBURequest().command(type);
	}

	/**
	 * 接受A3搜索OBU指令并且解析
	 *
	 * @param context
	 * @return A3Response
	 * @throws NoSuchElementException
	 */
	public SearchOBUResponse adoptA3(String context)
			throws NoSuchElementException {
		SearchOBUResponse dr = new SearchOBUResponse().analysis(context);
		if (bun == null)
			bun = new Bundle();
		if (bundle == null)
			bundle = new Bundle();
		if (checkCard == 0) {
			try {
				// 取高字节转换八位二进制
				int i = Integer.parseInt(dr.getOBUStatus().substring(0, 2), 16);
				String is = EncodingUtil
						.jointZero(Integer.toBinaryString(i), 8);
				// 0- OBU未被拆动，1-OBU被拆
				if ("0".equals(is.substring(6, 7))) {
					dr.setDismantle("01");
				} else {
					dr.setDismantle("00");
				}

				// 卡片是否存在
				isExist = is.substring(0, 1);
				// 系统信息
				String tagNumbers = "未定义";
				String OBUContract = tagNumbers;
				String OBUSecret = tagNumbers;
				String tagValidity_1 = tagNumbers;
				String tagValidity_2 = tagNumbers;
				if (dr.getSystemInfoLen().equals("0")) {
					tagNumbers = "";
					OBUContract = tagNumbers;
					OBUSecret = tagNumbers;
					tagValidity_1 = tagNumbers;
					tagValidity_2 = tagNumbers;
				} else {
					try {
						// 标签号码 系统信息合约序列号11-18字节
						tagNumbers = dr.getSysInfo().substring(20, 36);
					} catch (Exception e) {
						tagNumbers = "未定义";
					}

					try {
						// 标签有效期 系统信息的19-26字节
						tagValidity_1 = EncodingUtil.convert(dr.getSysInfo()
								.substring(36, 52));
						dr.setSignDate(dr.getSysInfo().substring(36, 44));
						dr.setDueDate(dr.getSysInfo().substring(44, 52));
						tagValidity_2 = tagValidity_1.substring(11);
						tagValidity_1 = tagValidity_1.substring(0, 11);
					} catch (Exception e) {
						tagValidity_2 = "未定义";
						tagValidity_1 = "未定义";
					}
					try {
						// OBU合同版本 系统信息的第10字节合同版本
						OBUContract = dr.getSysInfo().substring(18, 20);
						dr.setContractVersion(OBUContract);
					} catch (Exception e) {
						OBUContract = "未定义";
					}
					try {
						// OBU密钥版本 系统信息的第10字节合同版本
						OBUSecret = dr.getSysInfo().substring(18, 20);
					} catch (Exception e) {
						OBUSecret = "未定义";
					}
				}
				// OBU MAC地址 BA指令返回的4字节OBUMAC
				bun.putString("OBUMAC1", dr.getObuMac());
				// 标签号码
				bun.putString("TAGNUMBERS1", tagNumbers);
				// 标签到期时间
				bun.putString("TAGVALIDITY1", tagValidity_1);
				bun.putString("TAGVALIDITY2", tagValidity_2);
				// 标签合同版本
				bun.putString("OBUCONTRACT1", OBUContract);
				// 标签密钥版本
				bun.putString("OBUSECRET1", OBUSecret);
				// 拆卸状态 OBUStatus的第一字节的Bit1位，false：未拆，true：拆卸
				bun.putString("OBUSTATUS1", is.substring(6, 7));
				// 电池电量 OBUStatus的第一字节的Bit0位，false：正常，true：低电
				bun.putString("OBUBATTERY", is.substring(7));

			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (checkCard == 1) {
			try {
				// 取高字节转换八位二进制
				int i = Integer.parseInt(dr.getOBUStatus().substring(0, 2), 16);
				String is = EncodingUtil
						.jointZero(Integer.toBinaryString(i), 8);
				// 0- OBU未被拆动，1-OBU被拆
				if ("0".equals(is.substring(6, 7))) {
					dr.setDismantle("01");
				} else {
					dr.setDismantle("00");
				}

				// 卡片是否存在
				isExist = is.substring(0, 1);
				// 系统信息
				String tagNumbers = "未定义";
				String OBUContract = tagNumbers;
				String OBUSecret = tagNumbers;
				String tagValidity_1 = tagNumbers;
				String tagValidity_2 = tagNumbers;
				if (dr.getSystemInfoLen().equals("0")) {
					tagNumbers = "";
					CardNumber = "";
					OBUContract = tagNumbers;
					OBUSecret = tagNumbers;
					tagValidity_1 = tagNumbers;
					tagValidity_2 = tagNumbers;
				} else {
					try {
						// 标签号码 系统信息合约序列号11-18字节
						tagNumbers = dr.getSysInfo().substring(20, 36);

					} catch (Exception e) {
						tagNumbers = "未定义";
					}
					// 记录下OBUMAC地址
					OBUMAC = dr.getObuMac();
					try {
						// 标签有效期 系统信息的19-26字节
						tagValidity_1 = EncodingUtil.convert(dr.getSysInfo()
								.substring(36, 52));
						dr.setSignDate(dr.getSysInfo().substring(36, 44));
						dr.setDueDate(dr.getSysInfo().substring(44, 52));
						tagValidity_2 = tagValidity_1.substring(11);
						tagValidity_1 = tagValidity_1.substring(0, 11);
					} catch (Exception e) {
						tagValidity_2 = "未定义";
						tagValidity_1 = "未定义";
					}
					try {
						// OBU合同版本 系统信息的第10字节合同版本
						OBUContract = dr.getSysInfo().substring(18, 20);
						dr.setContractVersion(OBUContract);
					} catch (Exception e) {
						OBUContract = "未定义";
					}
					try {
						// OBU密钥版本 系统信息的第10字节合同版本
						OBUSecret = dr.getSysInfo().substring(18, 20);
					} catch (Exception e) {
						OBUSecret = "未定义";
					}

				}
				// OBU MAC地址 BA指令返回的4字节OBUMAC
				bun.putString("OBUMAC1", dr.getObuMac());
				// 记录下OBUMAC地址
				OBUMAC = dr.getObuMac();
				// 标签号码
				bun.putString("TAGNUMBERS1", tagNumbers);
				// 标签到期时间
				bun.putString("TAGVALIDITY1", tagValidity_1);
				bun.putString("TAGVALIDITY2", tagValidity_2);
				// 标签合同版本
				bun.putString("OBUCONTRACT1", OBUContract);
				// 标签密钥版本
				bun.putString("OBUSECRET1", OBUSecret);
				// 拆卸状态 OBUStatus的第一字节的Bit1位，false：未拆，true：拆卸
				bun.putString("OBUSTATUS1", is.substring(6, 7));
				// 电池电量 OBUStatus的第一字节的Bit0位，false：正常，true：低电
				bun.putString("OBUBATTERY", is.substring(7));
				// 卡内车牌号
				String cardvehicleNumber = "";
				// 卡片序列号
				String cardNumber = "";
				// 卡片版本
				String cardCon = "";
				// 车卡绑定
				String isVehicleDoing = "";
				// 卡片有效期
				String time1 = "";
				String time2 = "";
				// 卡片类型
				String cardtype = "";
				// 卡信息存在
				if (dr.getCardInfoLen().equals("0")) {
					bundle.putString("CARDBINDINGVEHICLE1", "");
					bundle.putString("VEHICLECARDBINDING1", "");
					bundle.putString("CARDSECRET1", "");
					bundle.putString("CARDNUMBER1", "");
				} else {
					try {
						cardtype = EncodingUtil.hexToDec(dr.getCardInfo()
								.substring(0, 2), 2);
					} catch (Exception e) {
						cardtype = "未定义";
					}
					try {
						// 卡片有效期 卡片信息的21-28字节
						time1 = EncodingUtil.convert(dr.getCardInfo()
								.substring(24, 40));
						time2 = time1.substring(11);
						time1 = time1.substring(0, 11);

					} catch (Exception e) {
						time1 = "未定义";
						time2 = "未定义";
					}
					isVehicleDoing = dr.getCardInfo().substring(64, 66);
					vehicleCardBing = dr.getCardInfo().substring(64, 66);
					try {
						// 获得未转码的卡内车牌号
						bundle.putString("CardVehicle", dr.getCardInfo()
								.substring(40, 64));
						dr.setIsVehicleCardBinding(isVehicleDoing);
					} catch (Exception e) {
						bundle.putString("CardVehicle", "未定义");
					}
					cardCon = dr.getCardInfo().substring(2, 4);
					dr.setCardConver(cardCon);
					try {
						cardNumber = dr.getCardInfo().substring(4, 24);
						CardNumber = cardNumber;
						dr.setCardNumber(cardNumber);
					} catch (Exception e) {
						cardNumber = "FFFFFFFFFFFFFFFFFFFF";
					}
					try {
						dr.setCardVehicleNumber(dr.getCardInfo().substring(40,
								64));
						cardvehicleNumber = EncodingUtil.asciiToString(dr
								.getCardInfo().substring(40, 64));
					} catch (Exception e) {
						cardvehicleNumber = "未定义";
					}

				}
				bundle.putString("CARDTYPE1", cardtype);
				bundle.putString("CARDBINDINGVEHICLE1", cardvehicleNumber);
				bundle.putString("CARDBINDINGVEHICLE0", isVehicleDoing);
				// bundle.putString("CARDBINDINGVEHICLE0", isVehicleDoing);
				bundle.putString("CARDSECRET1", cardCon);
				bundle.putString("CARDNUMBER1", cardNumber);
				bundle.putString("CARDVALIDITY1", time1);
				bundle.putString("CARDVALIDITY2", time2);
				// 钱包信息
				String balance = "";
				if (dr.getCardRestMoneyLen().equals("0")) {
					balance = "";
				} else {
					// 余额
					balance = EncodingUtil.hexToDec(dr.getCardRestMoney(), 0);
					if (balance.length() > 6) {
						String src = balance.substring(balance.length() - 2);
						balance = balance.substring(0, balance.length() - 2);
						balance = balance + "." + src + "元";
					} else {
						balance = (Double.parseDouble(balance) / 100) + "元";
					}

				}
				bundle.putString("BALANCE1", balance);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dr;
	}

	/**
	 * 发送接受A4取随机数指令
	 *
	 * @param index
	 *            选择目录
	 * @param len
	 *            长度
	 * @throws NoSuchElementException
	 *             错误提示
	 */
	public String sendA4(String index, String len)
			throws NoSuchElementException {
		return new GetRandomRequest().command(index, len);
	}

	/**
	 * 接受解析A4取随机数指令
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public GetRandomResponse adoptA4(String context)
			throws NoSuchElementException {
		GetRandomResponse a4Response = new GetRandomResponse()
				.analysis(context);
		return a4Response;
	}

	/**
	 * 发送A5读系统信息指令（系统信息长度27字节）
	 *
	 * @param version
	 *            OBU版本
	 * @param type
	 *            厂商类型，当OBU合同版本为0x00或0x10时该字段填写金逸02，聚利03，其它填写00
	 * @throws NoSuchElementException
	 */
	public String sendA5(String version, String type)
			throws NoSuchElementException {
		return new ReadSystemRequest().command(version, type, "27");
	}

	/**
	 * 接受解析A5读系统信息指令
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public ReadSystemResponse adoptB5(String context)
			throws NoSuchElementException {
		ReadSystemResponse a5Response = new ReadSystemResponse()
				.analysis(context);
		a5Response.analysisSystemInfo(a5Response.getSysInfoData());
		bun.putString("TAGNUMBERS1", a5Response.getContractNumber());
		bun.putString("OBUSECRET1", a5Response.getObuVe());
		return a5Response;
	}

	/**
	 * 发送A6读取车辆信息文件指令(传入8字节随机数)
	 *
	 * @param version
	 *            OBU版本
	 * @param length
	 *            要读取的车辆信息长度
	 * @param random
	 *            使用的八字节随机数
	 * @param access
	 *            8字节的认证码，11版本此处填写服务器端返回认证码，12和16版本此处填写全0
	 * @throws NoSuchElementException
	 */
	public String sendA6(String version, String length, String random,
						 String access) throws NoSuchElementException {
		return new ReadVehicleRequest()
				.command(version, length, random, access);
	}

	/**
	 * 接受解析A6读取车辆信息文件指令
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public ReadVehicleResponse adoptA6(String context)
			throws NoSuchElementException {
		ReadVehicleResponse a6Response = new ReadVehicleResponse()
				.analysis(context);
		/*try {
			FrontMachineService.bundle = TerminalService.bun;
			if (FrontMachineService.bundle == null) {
				FrontMachineService.bundle = new Bundle();
			}
			// 开始解析车辆信息
			String cardNumber0001 = "";
			String vehicleType0001 = cardNumber0001;
			String vehicleColour001 = cardNumber0001;
			try {
				cardNumber0001 = EncodingUtil.asciiToString(a6Response
						.getVehiInfoData().substring(0, 24));

			} catch (Exception e) {
				cardNumber0001 = "未定义";
			}
			try {
				vehicleType0001 = EncodingUtil.vehicleType(a6Response
						.getVehiInfoData().substring(28, 30));
			} catch (Exception e) {
				vehicleType0001 = "未定义";
			}
			try {
				vehicleColour001 = ""
						+ Integer.parseInt(a6Response.getVehiInfoData()
								.substring(24, 28));
			} catch (Exception e) {
				vehicleColour001 = "未定义";
			}
			// 签内车牌号
			FrontMachineService.bundle.putString("VEHICLENUMBER1",
					cardNumber0001);
			FrontMachineService.bundle.putString("VEHICLENUMBER01",
					cardNumber0001);
			// 车辆类型
			FrontMachineService.bundle.putString("VEHICLETYPE01",
					vehicleType0001);
			FrontMachineService.bundle.putString("VEHICLETYPE1",
					vehicleType0001);
			// 车辆颜色
			FrontMachineService.bundle.putString("VEHICLECOLOR01",
					vehicleColour001);
			FrontMachineService.bundle.putString("VEHICLECOLOUR1",
					vehicleColour001);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		return a6Response;
	}

	/**
	 * 发送A7更新车辆或系统信息指令（内容需要传入）
	 *
	 * @param version
	 *            OBU版本
	 * @param type
	 *            厂商类型，当OBU合同版本为0x00或0x10时该字段填写金逸02，聚利03，其它填写00
	 * @param offset
	 *            写文件偏移量，只有当VendorType为金逸02时，该字段有效，且需要输入偏移量，其它则填写0 18
	 * @param Itype
	 *            要更新的类型 0系统信息 1车辆信息
	 * @param date
	 *            更新内容
	 * @throws NoSuchElementException
	 *             错误提示
	 */
	public String sendA7(String version, String type, String offset,
						 String Itype, String date) throws NoSuchElementException {
		return new UpdateInformationRequest().command(version, type, offset,
				Itype, date);
	}

	/**
	 * 接受解析A7更新车辆或系统信息指令
	 *
	 * @param context
	 * @throws NoSuchElementException
	 */
	public UpdateInformationResponse adoptA7(String context)
			throws NoSuchElementException {
		UpdateInformationResponse a7Response = new UpdateInformationResponse()
				.analysis(context);
		return a7Response;
	}

	/**
	 * 发送A8指令释放OBU
	 *
	 * @param type
	 *            交易是否成功 0表示交易成功释放，1表示交易异常释放
	 * @return
	 */
	public String sendA8(String type) {
		return new CloseOBUConnectionRequest().command(type);
	}

	/**
	 * 解析A8释放OBU指令
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public CloseOBUConnectionResponse adoptA8(String context)
			throws NoSuchElementException {
		CloseOBUConnectionResponse cr = new CloseOBUConnectionResponse()
				.analysiss(context);
		return cr;
	}

	/**
	 * 发送A9指令参数设置
	 *
	 * @return
	 * @throws NoSuchElementException
	 */
	public String sendA9() throws NoSuchElementException {
		//
		return new SetParameterRequest().command(
				Connection.map.get("TxChannel").substring(0, 1),
				Connection.map.get("RxChannel").substring(0, 1),
				Connection.map.get("TxPower"),
				Connection.map.get("RxPower"), Connection.map.get("CRC"),
				Connection.map.get("PsamNumForVersion1"),
				Connection.map.get("PsamNumForVersion2"),
				Connection.map.get("BstNumber"),
				Connection.map.get("BstInterval"),
				Connection.map.get("TrasferChannelNumber"),
				Connection.map.get("TrasferChannelInterval"),
				Connection.map.get("PsamNumForVersion3"),
				Connection.map.get("PsamNumForVersion4"));
	}

	public String sendA9_2() throws NoSuchElementException {
		return new SetParameterRequest().command(
				Connection.map.get("TxChannel").substring(0, 1),
				Connection.map.get("RxChannel").substring(0, 1),
				Connection.map.get("TxPower"),
				Connection.map.get("RxPower"), Connection.map.get("CRC"),
				Connection.map.get("PsamNumForVersion1"),
				Connection.map.get("PsamNumForVersion2"),
				"200",//Connection.map.get("BstNumber"),
				"10",//Connection.map.get("BstInterval"),
				"100",//Connection.map.get("TrasferChannelNumber"),
				"10",//Connection.map.get("TrasferChannelInterval"),
				Connection.map.get("PsamNumForVersion3"),
				Connection.map.get("PsamNumForVersion4"));
	}

	/**
	 * 解析B9指令参数设置
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public SetParameterResponse adoptA9(String context)
			throws NoSuchElementException {
		SetParameterResponse sr = new SetParameterResponse().analysis(context);
		return sr;
	}

	/**
	 * 发送AA指令检测OBU
	 *
	 * @throws NoSuchElementException
	 *             错误消息
	 */
	public String sendAA() {
		return new DetectionOBURequest().command();
	}

	/**
	 * 接受解析AA指令检测OBU
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public DetectionOBUResponse adoptAA(String context)
			throws NoSuchElementException {
		DetectionOBUResponse dr = new DetectionOBUResponse().analysis(context);
		bun = new Bundle();
		bundle = new Bundle();
		try {
			// 取高字节转换八位二进制
			String is = EncodingUtil.getBooleanArray(Byte.parseByte(dr
					.getOBUStatus().substring(0, 2)));
			bun = new Bundle();
			bundle = new Bundle();
			// 没有系统信息
			String tagNumbers = "未定义";
			String OBUContract = tagNumbers;
			String OBUSecret = tagNumbers;
			String tagValidity_1 = tagNumbers;
			String tagValidity_2 = tagNumbers;
			if (dr.getSystemInfoLen().equals("0")) {
				tagNumbers = "";
				OBUContract = tagNumbers;
				OBUSecret = tagNumbers;
				tagValidity_1 = tagNumbers;
				tagValidity_2 = tagNumbers;
			} else {

				// 标签号码 系统信息合约序列号11-18字节
				try {
					tagNumbers = dr.getSysInfo().substring(20, 36);
				} catch (Exception e) {
					tagNumbers = "未定义";
				}
				try {
					// 标签有效期 系统信息的19-26字节
					tagValidity_1 = EncodingUtil.convert(dr.getSysInfo()
							.substring(36, 52));
					tagValidity_2 = tagValidity_1.substring(11);
					tagValidity_1 = tagValidity_1.substring(0, 11);
				} catch (Exception e) {
					tagValidity_1 = "未定义";
					tagValidity_2 = tagValidity_1;
				}
				try {
					// OBU合同版本 系统信息的第10字节合同版本
					OBUContract = dr.getSysInfo().substring(18, 20);
				} catch (Exception e) {
					OBUContract = "未定义";
				}
				try {
					// OBU密钥版本 系统信息的第10字节合同版本
					OBUSecret = dr.getSysInfo().substring(18, 20);
				} catch (Exception e) {
					OBUSecret = "未定义";
				}

			}
			bun.putString("TAGNUMBERS1", tagNumbers);
			bun.putString("TAGVALIDITY1", tagValidity_1);
			bun.putString("TAGVALIDITY2", tagValidity_2);
			bun.putString("OBUCONTRACT1", OBUContract);
			bun.putString("OBUSECRET1", OBUSecret);
			// 如果没有车辆信息
			String vehicleNumber = "未定义";
			String vehicleType = vehicleNumber;
			String vehicleColour = vehicleNumber;
			if (dr.getVehicleInfoLen().equals("0")) {
				vehicleNumber = "";
				vehicleType = vehicleNumber;
				vehicleColour = vehicleNumber;
			} else {
				try {
					// 签内车牌号 车辆信息的1-12字节车牌信息
					vehicleNumber = EncodingUtil.asciiToString(dr
							.getVehicleInfo().substring(0, 24));
				} catch (Exception e) {
					vehicleNumber = "未定义";
				}
				try {
					// 车牌颜色 车辆信息13-14个字节
					vehicleColour = Integer.parseInt(dr.getVehicleInfo()
							.substring(24, 28)) + "";
				} catch (Exception e) {
					vehicleColour = "未定义";
				}
				try {
					// 车型 车辆信息的15字节
					vehicleType = EncodingUtil.hexToDec(dr.getVehicleInfo()
							.substring(28, 30), 2);
				} catch (Exception e) {
					vehicleType = "未定义";
				}
			}
			bun.putString("VEHICLENUMBER1", vehicleNumber);
			bun.putString("VEHICLETYPE1", vehicleType);
			bun.putString("VEHICLECOLOUR1", vehicleColour);
			// 拆卸状态 OBUStatus的第一字节的Bit1位，false：未拆，true：拆卸
			bun.putString("OBUSTATUS1", is.substring(6, 7));
			// 电池电量 OBUStatus的第一字节的Bit0位，false：正常，true：低电
			bun.putString("OBUBATTERY", is.substring(7));
			// OBU MAC地址 BA指令返回的4字节OBUMAC
			bun.putString("OBUMAC1", dr.getObuMac());

			String vehicleCardBinding = "未定义";
			String cardBindingVehicle = vehicleCardBinding;
			String cardValidity_1 = vehicleCardBinding;
			String cardValidity_2 = vehicleCardBinding;
			String cardSecret = vehicleCardBinding;
			String cardNumber = vehicleCardBinding;
			String cardType = vehicleCardBinding;
			if (dr.getCardInfoLen().equals("0")) {
				// 没有卡片信息
				vehicleCardBinding = "";
				cardBindingVehicle = vehicleCardBinding;
				cardValidity_1 = vehicleCardBinding;
				cardValidity_2 = vehicleCardBinding;
				cardSecret = vehicleCardBinding;
				cardNumber = vehicleCardBinding;
				cardType = vehicleCardBinding;
			} else {
				try {
					// 车卡绑定 卡片信息的41字节 0：普通用户 01：车卡绑定
					vehicleCardBinding = dr.getCardInfo().substring(80, 82);
				} catch (Exception e) {
					vehicleCardBinding = "未定义";
				}

				try {
					// 卡内车牌号 卡信息29-40字节的车牌信息
					cardBindingVehicle = EncodingUtil.asciiToString(dr
							.getCardInfo().substring(56, 80));
				} catch (Exception e) {
					cardBindingVehicle = "未定义";
				}
				try {
					// 卡片有效期 卡片信息的21-28字节
					cardValidity_1 = EncodingUtil.convert(dr.getCardInfo()
							.substring(40, 56));
					cardValidity_2 = cardValidity_1.substring(11);
					cardValidity_1 = cardValidity_1.substring(0, 11);

				} catch (Exception e) {
					cardValidity_2 = "未定义";
					cardValidity_1 = cardValidity_2;
				}
				try {
					// 卡片密钥版本 卡片信息中的第10字节
					cardSecret = dr.getCardInfo().substring(18, 20);
				} catch (Exception e) {
					cardSecret = "未定义";
				}
				try {
					// 卡号 卡信息的13-20字节CPU卡片内部编号
					cardNumber = dr.getCardInfo().substring(20, 40);
				} catch (Exception e) {
					cardNumber = "未定义";
				}
				try {
					// 卡类型 卡信息的第9字节卡类型
					cardType = EncodingUtil.hexToDec(dr.getCardInfo()
							.substring(16, 18), 2);
				} catch (Exception e) {
					cardType = "未定义";
				}
			}
			bundle.putString("CARDBINDINGVEHICLE0", vehicleCardBinding);
			bundle.putString("CARDBINDINGVEHICLE1", cardBindingVehicle);
			bundle.putString("CARDVALIDITY1", cardValidity_1);
			bundle.putString("CARDVALIDITY2", cardValidity_2);
			bundle.putString("CARDSECRET1", cardSecret);
			bundle.putString("CARDNUMBER1", cardNumber);
			bundle.putString("CARDTYPE1", cardType);

			String balance = "0";
			if (dr.getCardRestMoneyLen().equals("0")) {
				// 没有卡内余额信息
				balance = "";
			} else {
				// 余额
				balance = EncodingUtil.hexToDec(dr.getCardRestMoney(), 0);
				if (balance.length() > 6) {
					String src = balance.substring(balance.length() - 2);
					balance = balance.substring(0, balance.length() - 2);
					balance = balance + "." + src + "元";
				} else {
					balance = (Double.parseDouble(balance) / 100) + "元";
				}
			}
			bundle.putString("BALANCE1", balance);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dr;
	}

	/**
	 * 发送激活OBU幀请求
	 *
	 * @param IsModifySignedDate
	 *            是否修改启用日期，1表示修改，0表示不修改
	 * @param SignedDate
	 *            启用日期
	 * @param IsModifyExpiredDate是否修改失效日期
	 *            ，1表示修改，0表示不修改
	 * @param ExpiredDate
	 *            失效日期
	 * @return
	 * @throws NoSuchElementException
	 */
	public String sendAB() throws NoSuchElementException {
		String SignedDate = Connection.map.get("SignedDate");
		String IsModifyExpiredDate = Connection.map
				.get("IsModifyExpiredDate");
		String ExpiredM = Connection.map.get("ExpiredM");
		String ExpiredN = Connection.map.get("ExpiredN");
		String IsExpiredN = Connection.map.get("IsExpiredN");
		String IsModifySignedDate = Connection.map.get("IsModifySignedDate");
		if ("00".equals(IsModifyExpiredDate) || "0".equals(IsModifyExpiredDate)) {
			IsExpiredN = "00";
			ExpiredM = "00";
			ExpiredN = "00";
		}
		IsExpiredN = Connection.map.get("IsExpiredN");
		return new ActivateOBURequest().command(IsModifySignedDate, SignedDate,
				IsModifyExpiredDate, ExpiredM, ExpiredN, IsExpiredN);

	}

	/**
	 * 测试
	 *
	 * @return
	 */
	public String sendABB() {
		return new ActivateOBURequest().command("1", "20130601", "1", "255",
				"255", "1");
	}

	/**
	 * 接受解析BB激活OBU应答幀
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public ActivateOBUResponse adoptAB(String context)
			throws NoSuchElementException {
		ActivateOBUResponse ar = new ActivateOBUResponse().analysiss(context);
		try {
			bun = new Bundle();
			bundle = new Bundle();
			// 没有系统信息
			String tagNumbers = "未定义";
			// String tagValidity_1 = tagNumbers;
			// String tagValidity_2 = tagNumbers;
			// String OBUContract = tagNumbers;
			String OBUSecret = tagNumbers;
			try {
				// 标签号码 系统信息合约序列号11-18字节
				tagNumbers = ar.getContractSerialNumber();
			} catch (Exception e) {
				tagNumbers = "未定义";
			}
			try {
				// OBU密钥版本 系统信息的第10字节合同版本
				OBUSecret = ar.getContractVersion();
			} catch (Exception e) {
				OBUSecret = "未定义";
			}
			// 标签有效期 系统信息的19-26字节
			// Log.e("标签有效期", ar.getSysInfo().substring(36, 52));
			// tagValidity_1 =
			// EncodingUtil.convert(ar.getSysInfo().substring(36,
			// 52));
			// tagValidity_2 = tagValidity_1.substring(11);
			// tagValidity_1 = tagValidity_1.substring(0, 11);
			// OBU合同版本 系统信息的第10字节合同版本
			// OBUContract = ar.getSysInfo().substring(18, 20);

			bun.putString("tagNumbers01", tagNumbers);
			// bundle.putString("tagValidity_01", tagValidity_1);
			// bundle.putString("tagValidity_02", tagValidity_2);
			// bundle.putString("OBUContract01", OBUContract);
			bun.putString("OBUSecret01", OBUSecret);
			// 如果没有车辆信息
			String vehicleNumber = "未定义";
			String vehicleType = vehicleNumber;
			String vehicleColour = vehicleNumber;
			try {
				// 签内车牌号 车辆信息的1-12字节车牌信息
				vehicleNumber = EncodingUtil.asciiToString(ar
						.getVehicleNumber());
			} catch (Exception e) {
				vehicleNumber = "未定义";
			}
			try {
				// 车牌颜色 车辆信息13-14个字节
				vehicleColour = Integer.parseInt(ar.getVehicleColor()) + "";
			} catch (Exception e) {
				vehicleColour = "未定义";
			}
			try {
				// 车型 车辆信息的15字节
				vehicleType = EncodingUtil.hexToDec(ar.getVehicleClass(), 2);
			} catch (Exception e) {
				vehicleType = "未定义";
			}

			bun.putString("vehicleNumber01", vehicleNumber);
			bun.putString("vehicleType01", vehicleType);
			bun.putString("vehicleColour01", vehicleColour);
			// 没有卡片信息
			// String vehicleCardBinding = "未定义";
			String cardBindingVehicle = "未定义";
			// String cardValidity_1 = vehicleCardBinding;
			// String cardValidity_2 = vehicleCardBinding;
			String cardSecret = cardBindingVehicle;
			// String cardNumber = "未定义";
			String cardType = cardBindingVehicle;

			// 车卡绑定 卡片信息的41字节 0：普通用户 01：车卡绑定
			// vehicleCardBinding = ar.getCardInfo().substring(82, 84);
			try {
				// 卡内车牌号 卡信息29-40字节的车牌信息
				cardBindingVehicle = EncodingUtil.asciiToString(ar
						.getCardVehicleNumber());
			} catch (Exception e) {
				cardBindingVehicle = "未定义";
			}
			// 卡片有效期 卡片信息的21-28字节
			// cardValidity_1 = EncodingUtil.convert(ar.getCardInfo()
			// .substring(42, 58));
			// cardValidity_2 = cardValidity_1.substring(11);
			// cardValidity_1 = cardValidity_1.substring(0, 11);
			// Log.e("卡片时间1", cardValidity_1);
			// Log.e("卡片时间2", cardValidity_2);
			// 卡片密钥版本 卡片信息中的第10字节
			try {
				cardSecret = ar.getCardVersion();
			} catch (Exception e) {
				cardSecret = "未定义";
			}
			// 卡号 卡信息的13-20字节CPU卡片内部编号
			// cardNumber = ar.getCardInfo().substring(26, 42);
			// 卡类型 卡信息的第9字节卡类型
			try {
				cardType = EncodingUtil.sixToTen(ar.getCardType());
			} catch (Exception e) {
				cardType = "未定义";
			}
			// bundle.putString("vehicleCardBinding01", vehicleCardBinding);
			bundle.putString("CARDTIME", ar.getStopTime());
			bundle.putString("cardBindingVehicle01", cardBindingVehicle);
			bundle.putString("CARDBINDINGVEHICLE0", cardBindingVehicle);
			// bundle.putString("cardValidity_01", cardValidity_1);
			// bundle.putString("cardValidity_02", cardValidity_2);
			bundle.putString("cardSecret01", cardSecret);
			// bundle.putString("cardNumber01", cardNumber);
			bundle.putString("cardType01", cardType);
		} catch (Exception e) {

		}
		return ar;
	}

	/**
	 * 发送AC指令OBU卡片通道命令
	 *
	 * @param number
	 *            重发次数 20
	 *
	 * @param inval
	 *            重发间隔 15
	 * @param type
	 *            1:OBU中的卡进行操作，2:对OBU中的ESAM进行操作
	 * @param Apdu1Cmd
	 *            长度+读卡命令 00B0950821
	 * @return
	 */
	public String sendAC(String number, String inval, String type,
						 String... Apdu1Cmd) {
		return new OBUCardCommandChannelRequest().command(number, inval, type,
				Apdu1Cmd);
	}

	/**
	 * 解析BCOBU卡片通道命令应答
	 *
	 * @param context
	 * @return
	 * @throws Exception
	 */
	public OBUCardCommandChannelResponse adoptBC(String context)
			throws Exception {
		OBUCardCommandChannelResponse or = new OBUCardCommandChannelResponse()
				.analysis(context);
		if (or == null) {
			return null;
		}
		if (bundle == null)
			bundle = new Bundle();
		if (type.equals("1")) {
			// 卡片信息
			String vehicleCardBinding = "未定义";
			String cardBindingVehicle = vehicleCardBinding;
			String cardValidity_1 = vehicleCardBinding;
			String cardValidity_2 = vehicleCardBinding;
			String cardSecret = vehicleCardBinding;
			String cardNumber = vehicleCardBinding;
			String cardType = vehicleCardBinding;
			if (or.getApdu1Length()[0].equals("0")) {
				vehicleCardBinding = "";
				cardBindingVehicle = vehicleCardBinding;
				cardValidity_1 = vehicleCardBinding;
				cardValidity_2 = vehicleCardBinding;
				cardSecret = vehicleCardBinding;
				cardNumber = vehicleCardBinding;
				cardType = vehicleCardBinding;
			} else {
				// 卡片信息
				String cardInfo = or.getApdu1Data()[0];
				try {
					// 车卡绑定 卡片信息的41字节 0：普通用户 01：车卡绑定
					vehicleCardBinding = cardInfo.substring(64, 66);
					or.setBingCardVehicle(cardInfo.substring(64, 66));
					vehicleCardBing = vehicleCardBinding;
				} catch (Exception e) {
					vehicleCardBinding = "未定义";
				}
				try {
					// 记录卡内车牌号
					or.setCardVehicleNumber(cardInfo.substring(40, 64));
				} catch (Exception e) {

				}

				// 获得未转码的卡内车牌号
				bundle.putString("CardVehicle", cardInfo.substring(40, 64));
				try {
					// 卡内车牌号 卡信息29-40字节的车牌信息
					cardBindingVehicle = EncodingUtil.asciiToString(cardInfo
							.substring(40, 64));
				} catch (Exception e) {
					cardBindingVehicle = "未定义";
				}
				// 卡片有效期 卡片信息的21-28字节
				cardValidity_1 = EncodingUtil.convert(cardInfo
						.substring(24, 40));
				cardValidity_2 = cardValidity_1.substring(11);
				cardValidity_1 = cardValidity_1.substring(0, 11);
				// 卡片密钥版本 卡片信息中的第10字节
				cardSecret = cardInfo.substring(2, 4);
				or.setCardVersion(cardSecret);
				// 卡号 卡信息的13-20字节CPU卡片内部编号
				cardNumber = cardInfo.substring(4, 24);
				CardNumber = cardNumber;

				or.setCardNumber(cardNumber);

				// 卡类型 卡信息的第9字节卡类型
				cardType = EncodingUtil.hexToDec(cardInfo.substring(0, 2), 2);

				bundle.putString("VEHICLECARDBINDING1", vehicleCardBinding);
				bundle.putString("CARDBINDINGVEHICLE1", cardBindingVehicle);
				bundle.putString("CARDBINDINGVEHICLE0", cardBindingVehicle);
				bundle.putString("CARDVALIDITY1", cardValidity_1);
				bundle.putString("CARDVALIDITY2", cardValidity_2);
				bundle.putString("CARDSECRET1", cardSecret);
				bundle.putString("CARDNUMBER1", cardNumber);
				bundle.putString("CARDTYPE1", cardType);
			}
			if (or.getApdu1Length()[0].equals("0")) {
				return null;
			}
		} else if (type.equals("2")) {
			// 卡片信息
			String vehicleCardBinding = "未定义";
			String cardBindingVehicle = vehicleCardBinding;
			String cardValidity_1 = vehicleCardBinding;
			String cardValidity_2 = vehicleCardBinding;
			String cardSecret = vehicleCardBinding;
			String cardNumber = vehicleCardBinding;
			String cardType = vehicleCardBinding;
			if (or.getApdu1Length()[0].equals("0")) {
				vehicleCardBinding = "";
				cardBindingVehicle = vehicleCardBinding;
				cardValidity_1 = vehicleCardBinding;
				cardValidity_2 = vehicleCardBinding;
				cardSecret = vehicleCardBinding;
				cardNumber = vehicleCardBinding;
				cardType = vehicleCardBinding;
			} else {
				// 卡片信息
				String cardInfo = or.getApdu1Data()[0];
				// 车卡绑定 卡片信息的41字节 0：普通用户 01：车卡绑定
				or.setBingCardVehicle(cardInfo.substring(64, 66));
				or.setCardVehicleNumber(cardInfo.substring(40, 64));
				// 获得未转码的卡内车牌号
				bundle.putString("CardVehicle", cardInfo.substring(40, 64));
				try {
					// 卡内车牌号 卡信息29-40字节的车牌信息
					cardBindingVehicle = EncodingUtil.asciiToString(cardInfo
							.substring(40, 64));
				} catch (Exception e) {
					cardBindingVehicle = "未定义";
				}
				// 卡片有效期 卡片信息的21-28字节
				cardValidity_1 = EncodingUtil.convert(cardInfo
						.substring(24, 40));
				cardValidity_2 = cardValidity_1.substring(11);
				cardValidity_1 = cardValidity_1.substring(0, 11);
				// 卡片密钥版本 卡片信息中的第10字节
				cardSecret = cardInfo.substring(2, 4);
				or.setCardVersion(cardSecret);
				// 卡号 卡信息的13-20字节CPU卡片内部编号
				cardNumber = cardInfo.substring(4, 24);
				CardNumber = cardNumber;
				or.setCardNumber(cardNumber);
				// 卡类型 卡信息的第9字节卡类型
				cardType = EncodingUtil.hexToDec(cardInfo.substring(0, 2), 2);
				bundle.putString("VEHICLECARDBINDING1", vehicleCardBinding);
				bundle.putString("CARDBINDINGVEHICLE1", cardBindingVehicle);
				bundle.putString("CARDBINDINGVEHICLE0", cardBindingVehicle);
				bundle.putString("CARDVALIDITY1", cardValidity_1);
				bundle.putString("CARDVALIDITY2", cardValidity_2);
				bundle.putString("CARDSECRET1", cardSecret);
				bundle.putString("CARDNUMBER1", cardNumber);
				bundle.putString("CARDTYPE1", cardType);
			}

			// 没有卡内余额信息
			String balance = "0";
			if (or.getApdu1Length()[1].equals("0")) {
				balance = "";
			} else {
				// 余额
				balance = EncodingUtil.hexToDec(or.getApdu1Data()[1], 0);
				if (balance.length() > 6) {
					String src = balance.substring(balance.length() - 2);
					balance = balance.substring(0, balance.length() - 2);
					balance = balance + "." + src + "元";
				} else {
					balance = (Double.parseDouble(balance) / 100) + "元";
				}
				bundle.putString("BALANCE1", balance);
			}
			if (or.getApdu1Length()[0].equals("0")
					|| or.getApdu1Length()[1].equals("0")) {
				return null;
			}
		}
		return or;
	}

	/**
	 * 发送硬件测试
	 *
	 * @param status
	 *            状态
	 * @param type
	 *            类型
	 * @return
	 */
	public String sendAd(String status, String type) {
		return new HardwareParamTextRequest().command(status, type);
	}

	/**
	 * 解析硬件测试
	 *
	 * @param context
	 *            内容
	 * @return
	 * @throws NoSuchElementException
	 */
	public HardwareParamTextResponse adoptBd(String context)
			throws NoSuchElementException {
		HardwareParamTextResponse hr = new HardwareParamTextResponse()
				.analysiss(context);
		return hr;
	}

	public String adoptBF(String context) throws NoSuchElementException {
		if (context != null) {
			// 判断状态是否成功
			if (!context.substring(12, 14).equals("BF")) {
				throw new NoSuchElementException("接收到的按键消息不正确");
			}
			String bcc = context.substring(context.length() - 2).toUpperCase();
			// 截取到发送的数据(总长度-2个字节计算到数据的最后位置)
			String str = context.substring(4, context.length() - 2);
			// 得到比对校验
			str = EncodingUtil.yihuo(EncodingUtil.change(str)).toUpperCase();
			// 判断校验是否一致
			if (!str.equals(bcc)) {
				throw new NoSuchElementException("接收到的按键消息不正确");
			}
			return context.substring(14, 16);
		} else {
			return null;
		}
	}

	/**
	 * 发送AE控制设备开关
	 *
	 * @param DevType
	 *            0表示非接触读卡电源，1表示微波芯片电源，2表示微波功放电源，3表示PSAM供电电源
	 * @param Switch
	 *            0表示电源关，1表示电源开
	 * @return
	 */
	public String sendAE(String DevType, String Switch) {
		return new WAVELOGRequest().command(DevType, Switch);
	}

	/**
	 * 解析设备开关
	 *
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public void adoptBE(String context) throws NoSuchElementException {
		new WAVELOGResponse().analysiss(context);
	}

	/**
	 * 微波收发指令组成
	 * @param dataCount重发次数 1byte
	 * @param interval数据接收超时时间 1byte
	 * @param dsrcData
	 * @return
	 */
	public String sendA5(String dataCount, String interval, String dsrcData){
		return new WaveSendRequest().command(dataCount, interval, dsrcData);
	}

	/**
	 * 微波收发指令解析
	 * @param context
	 * @return
	 * @throws NoSuchElementException
	 */
	public WaveSendResponse analysisB5(String context) throws NoSuchElementException {
		return new WaveSendResponse().analysis(context);
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
